// pages/zero-yuan-clap/clap.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    select:'',
    time:'',
    zerolist:'',
    auction:'',

    timer1:''
  },
  // 0元拍/秒杀/鉴宝/论坛-顶部图/优势图
  getways: function () {
    ajax('/api/index/config_list', {
      data: {
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {

      this.setData({
        auction: res.data.auction
      })
    })
  },
  // 0元拍列表
  getlist:function(){
    ajax('/api/index/auction_list', {
      data: {
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      for (var x in res.data){

        var timelist = res.data[x].time_diff.split(":");

        res.data[x].shi = timelist[0]
        res.data[x].fen = timelist[1]
        res.data[x].miao = timelist[2]


        if (res.data[x].is_selected==1){
          this.setData({
            select:x
          })
        }

      }
      this.setData({
        zerolist: res.data,
      })

      this.settime()
    })
  },


  // 定时器  一秒走一次
  settime: function () {
    var that = this
    that.data.timer1 = setInterval(function () {
      for (var x in that.data.zerolist){

        if (that.data.zerolist[x].status!=2){
          var miao = that.data.zerolist[x].miao -= 1
          var shi = that.data.zerolist[x].shi
          var fen = that.data.zerolist[x].fen

          // console.log(miao, fen, shi, 'shifenmiao')

          if (miao <= 0) {
            fen -= 1
            miao = 60
          }
          if (fen == -1) {
            shi -= 1
            fen = 60
          }

          var str1 = 'zerolist[' + x + '].shi'
          var str2 = 'zerolist[' + x + '].fen'
          var str3 = 'zerolist[' + x + '].miao'
          that.setData({
            [str1]: shi,
            [str2]: fen,
            [str3]: miao,
          })
          if (shi == -1) {
            // clearInterval(that.timer1)
            that.getlist()
          }
        }
      }
    }, 1000)
  },




  // 提醒我
  submitInfo: function (e) {
    var id = e.currentTarget.dataset.id
    ajax('/api/user/remind', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        id: id,
        form_id: e.detail.formId
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "POST",
    }, res => {
      if (res.error == '0') {
        wx.showToast({
          title: '提醒成功',
          icon: 'success',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: res.msg,
          duration: 2000
        })
      }
    })
  },
  // // for循环
  // getfor:function(){
  //   for (var a in this.data.zerolist){
  //     if (this.data.zerolist[a].is_selected == 1){
  //       this.setData({
  //         select: a,
  //         time: this.data.zerolist[a].time_diff
  //       })
  //       this.altertime()
  //     }
  //   }
  // },
  // // 分隔  时分秒  剩余时间  
  // altertime: function () {
  //   var time = this.data.time;
  //   var arr = time.split(":");
  //   this.setData({
  //     shi: arr[0],
  //     fen: arr[1],
  //     miao: arr[2]
  //   })
  // },
  // 选择
  active:function(e){
    var idx = e.currentTarget.dataset.index;
    this.setData({
      select: idx
    })
    this.setData({
      time: this.data.zerolist[this.data.select].time_diff
    })
  },
  // 0元拍详情
  godetails:function(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/zero-yuan-clap-details/details?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getways()
    this.getlist()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.timer1)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timer1)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})